/**
 * Created by dukes on 2015/10/20.
 */
class DailyMissionItemRender extends eui.ItemRenderer{
    constructor(){
        super();
        this.skinName = "pet.DailyMissionItemRenderSkin";
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.addedToStage, this );
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.removedFromStage, this);
    }

    private addedToStage(evt:egret.Event){
        this.img_get.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGet,this);
    }

    private removedFromStage(evt:egret.Event){
        this.img_get.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onGet,this);
    }

    private onGet(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        window["__missionAward"](this.data);
    }

    public lab_desc:eui.Label;
    public lab_progress:eui.Label;
    public img_money:eui.Image;
    public group_money:eui.BitmapLabel;
    public img_complete:eui.Image;
    public img_get:eui.Image;
    public partAdded(name:string, instance:any){
        super.partAdded(name, instance);
         if( name == "lab_desc" ){
            this.lab_desc = instance;
        }else if( name == "img_money" ){
            this.img_money = instance;
        }else if( name == "group_money" ){
            this.group_money = instance;
             this.group_money.textAlign = "center";
        }else if( name == "img_get" ){
            this.img_get = instance;
        }else if( name == "img_complete" ){
            this.img_complete = instance;
        }else if( name == "lab_progress" ){
            this.lab_progress = instance;
        }
    }

    public dataChanged(){
        super.dataChanged();
        var mission:MissionInfo = Model.missionModels[this.data] || Model.unlockModels[this.data];
        var hairPrize:number = mission.dogHair || mission.catHair;
        this.lab_desc.text = mission.name;
        if(hairPrize != 0){
            this.img_money.source = mission.dogHair == 0 ? "img_money_cat" : "img_money_dog";
            // this.img_money.commitProperties();
            this.group_money.text = GetNumString(hairPrize,false,true);
            return;
        }
        if(mission.diamond!=0){
            this.img_money.source = "img_diamond";
            this.group_money.text = mission.diamond+"";
        }
        this.img_get.visible = mission.status  == 1;
        this.img_complete.visible = mission.status == 2 || Model.finishedMissions.indexOf(this.data) != -1;
        if( !this.img_get.visible && !this.img_complete.visible && Model.finishedMissions.indexOf(this.data) == -1) {
            this.lab_progress.visible = true;
            this.lab_progress.text =  mission.currStep+"/"+mission.steps;
        }else{
            this.lab_progress.visible = false;
        }
    }
}